﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_QLTheLoai : DevComponents.DotNetBar.Office2007Form
    {
        Form_Them_CapNhat_TheLoai frm;
        TheLoai_BUS theloai_bus;
        TheLoai_DTO theloai_dto;

        public Form_QLTheLoai()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_TheLoai();
            frm.KhiThemMoi += frm_KhiThemMoi;
            frm.Action = "Insert";
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_TheLoai();
            frm.KhiCapNhat += frm_KhiCapNhat;
            frm.Action = "Update";
            frm.ID = dtg_QLTheLoai.Rows[dong].Cells["Str_MaTheLoai"].Value.ToString();
            frm.ShowDialog();
        }
        void LoadDanhSachTheLoai()
        {
            theloai_bus = new TheLoai_BUS();
            dtg_QLTheLoai.DataSource = theloai_bus.LayDanhSachTheLoai();
        }
        void frm_KhiThemMoi()
        {
            LoadDanhSachTheLoai();
        }
        void frm_KhiCapNhat()
        {
            LoadDanhSachTheLoai();
        }

        private void Form_QLTheLoai_Load(object sender, EventArgs e)
        {
            dtg_QLTheLoai.AutoGenerateColumns = false;
            LoadDanhSachTheLoai();
        }
        int dong;
        private void dtg_QLTheLoai_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
        }

        List<string> MaTheLoai;        
        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaTheLoai = new List<string>();
            for (int i = 0; i < dtg_QLTheLoai.Rows.Count; i++)
            {
                if (dtg_QLTheLoai.Rows[i].Cells["CHECKBOX"].Value != null)
                    MaTheLoai.Add(dtg_QLTheLoai.Rows[i].Cells["Str_MaTheLoai"].Value.ToString());
            }
            if (MaTheLoai.Count == 0)
            {
                MessageBoxEx.Show("Bạn chưa chọn thể loại để xóa.", "Thông báo");
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa thể loại", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < MaTheLoai.Count; i++)
                {
                    theloai_dto = new TheLoai_DTO();
                    theloai_bus = new TheLoai_BUS();
                    theloai_dto.Str_MaTheLoai = MaTheLoai[i].ToString();
                    theloai_bus.XoaTheLoai(theloai_dto);
                }
                LoadDanhSachTheLoai();
                MaTheLoai.Clear();
            }
            else
            {
                for (int i = 0; i < dtg_QLTheLoai.Rows.Count; i++)
                {
                    dtg_QLTheLoai.Rows[i].Cells["CHECKBOX"].Value = false;
                }
            }
        }

        private void dtg_QLLoaiNhanVien_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnCapNhat_Click(sender, e);
        }

        private void dtg_QLTheLoai_Click(object sender, EventArgs e)
        {
            if (dtg_QLTheLoai.Rows[dong].Cells["CHECKBOX"].Value == null)
                dtg_QLTheLoai.Rows[dong].Cells["CHECKBOX"].Value = true;
            else
                dtg_QLTheLoai.Rows[dong].Cells["CHECKBOX"].Value = null;
        }
    }
}
